package com.weixin.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.weixin.bean.DinnerTable;

import com.weixin.service.DinnerTableService;
import com.weixin.service.impl.DinnerTableServiceimpl;

@WebServlet("/DinnerTableServlet/*")
public class DinnerTableServlet extends HttpServlet {

	private DinnerTableService dinnerTableService = new DinnerTableServiceimpl();

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String pathInfo = request.getPathInfo().substring(1);
		if (pathInfo.equals("listAllTable")) {
			listAllTable(request, response);
		} else if (pathInfo.equals("listTable")) {
			listTable(request, response);
		}else if(pathInfo.equals("tuizhuo")) {
			tuizhuo(request,response);
		}else if (pathInfo.equals("removeByIdJson")) {
			removeByIdJson(request, response);
		}else if(pathInfo.equals("addTable")) {
			addTable(request,response);
		}
	}

	private void addTable(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		DinnerTable dinnerTable=new DinnerTable();
		dinnerTable.setTableName(request.getParameter("tableName"));
		boolean b=dinnerTableService.save(dinnerTable);
		if(b) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
		
	}

	private void removeByIdJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));
		if(dinnerTableService.delete(id)){
			response.getWriter().print(1);
		} else {
			response.getWriter().print(0);
		}
	}

	private void tuizhuo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int tableId=Integer.valueOf(request.getParameter("tableId"));
		int tableStatus=0;
		Date orderDate=null;
		boolean b=dinnerTableService.update(tableId, tableStatus, orderDate);
		 if(b) {
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
	}

	// 1. 前台首页：显示所有未预定的餐桌
	private void listTable(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 查询所有未预定餐桌
		Gson gson = new Gson();
		Map map = new HashMap();
		response.setContentType("text/javascript;charset=utf-8");
		List<DinnerTable> list = dinnerTableService.findNoUseTable();

		for (int i = 1; i <= list.size(); i++) {
			map.put("" + i, list.get(i - 1));

		}

		map.put("0", list.size());
		response.getWriter().println(gson.toJson(map));
		/*
		 * List<DinnerTable> list = dinnerTableService.findNoUseTable(); // 保存到request
		 * request.setAttribute("listDinnerTable", list);
		 * request.getRequestDispatcher("/app/index.jsp").forward(request, response);
		 */
	}

	private void listAllTable(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/javascript; charset=utf-8");
		List<DinnerTable> dinnerTableList = dinnerTableService.getAllByPage1();
		Map map = new HashMap();
		map.put("total", dinnerTableList.size());
		map.put("rows", dinnerTableList);
		// Gson gson = new Gson();
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		response.getWriter().print(gson.toJson(map));
		// request.getRequestDispatcher("/back/student/student_list2.jsp").forward(request,
		// response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
